<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Browse extends MX_Controller {


	function __construct()
	{
		parent::__construct();
		$this->template->set_layout('listing');
		$data['Page'] = "LISTING";

		$this->load->model('category/category_model');
		$this->load->model('location/location_model');

		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar','header/nav_bar');
		//$this->template->set_partial('modal_window', 'footer/modal_window');

		//$this->load->spark('codeigniter-forensics');
		//$this->output->enable_profiler(true);

	}



	public function _remap($method, $params = array())
	{
		//--* META DATA----*/
		$data['site_title'] = $this->settings_model->siteInfo('web_title');
		$data['canonical'] = base_url();
		$this->template->title($data['site_title']);
		$this->template->set_metadata('keywords', $this->settings_model->siteInfo('keyword'));
		$this->template->set_metadata('description', $this->settings_model->siteInfo('description'));

		//--DEFINE SESSION DATA --//
		//unset($_SESSION['sess_cat']);

		switch($method){
			case 'practice':

				$data['catID'] = $this->commoncall->get_id($params[0]);
				$_SESSION['sess_cat'] = $data['catID'];

				$data['filter'] = 'STATE';

				
			
				//$data['cityID'] = $this->input->cookie('cook_city');
				//$cityID = $data['cityID'];
				/*if($city){
				
					$cityname = $this->location_model->get_city_data($city)->cityname;
					$this->input->set_cookie("cook_city", $city, time()+3600*30);
					$this->input->set_cookie("cook_cityname", $cityname, time()+3600*30);
				}*/
				
				$data['per_page'] = 5;
				$data['offset'] = 0;
				
				//$data['limit'] = 1;
				$data['para'] = 'CATEGORY';
				$data['pid'] = $data['catID'];

				
				
				
				
				$this->template->set_partial('listing', 'modules/listing');
				$this->template->set_partial('filter', 'modules/filter');
				
				$brCat = $this->category_model->getCategory_info($data['pid']);
				$this->template->set_breadcrumb('Home', '/')
				//->set_breadcrumb('Category','')
				->set_breadcrumb('Category','category/all')
				->set_breadcrumb($brCat->name,'');
				
				/*$this->template->set_breadcrumb('Home', '/')
				->set_breadcrumb('Category',$brCat->name, 'browse/practice/'.$this->commoncall->url($brCat->name).'-'.$brCat->id);
				*/
				$this->template->title($brCat->meta_title,$data['site_title']);
				$this->template->set_metadata('keywords', $brCat->meta_keyword);
				$this->template->set_metadata('description', $brCat->meta_description);
				$this->template->build('index',$data);
				break;
					
			case 'region':

				$data['filter'] = 'CITY';

				$data['stateID'] = $this->commoncall->get_id($params[0]);
				$_SESSION['sess_state'] = $data['stateID'];

				$data['per_page'] = 10;
				$data['offset'] = 0;
				$data['para'] = 'REGION';
				$data['pid'] = $data['stateID'];

				$this->template->set_partial('listing', 'modules/listing');
				$this->template->set_partial('filter', 'modules/filter');
				
			//	
			
				$statename = $this->location_model->get_region_data($data['pid'])->statename;
				$state = $this->location_model->get_region_data($data['pid']);
				$this->template->set_breadcrumb('Home', '/')
				//->set_breadcrumb('Category','')
				->set_breadcrumb('Region','location/region/all')
				->set_breadcrumb($statename,'');
				$this->template->title($state->meta_title,$data['site_title']);
				$this->template->set_metadata('keywords', $state->meta_keyword);
				$this->template->set_metadata('description', $state->meta_description);
				$this->template->build('index',$data);
				break;

			case 'city':

				$data['cityID'] = $this->commoncall->get_id($params[0]);
				$cityID = $data['cityID'];
				$_SESSION['sess_city'] = $cityID;

				$data['per_page'] = 10;
				$data['offset'] = 0;
				$data['para'] = 'CITY';
				$data['pid'] = $cityID;

				$this->template->set_partial('listing', 'modules/listing');
				
				
				$cityname = $this->location_model->get_city_data($data['pid'])->cityname;
				
				$city = $this->location_model->get_city_data($data['pid']);
				
				$this->input->set_cookie("cook_city", $cityID, time()+3600*30);
				$this->input->set_cookie("cook_cityname", $cityname, time()+3600*30);
				
				if (isset($_COOKIE["cook_city"])){
					setcookie("cook_city", $cityID, time()+3600,"/",$_COOKIE["cook_city"]);
					setcookie("cook_cityname", $cityname, time()+3600,"/",$_COOKIE["cook_city"]);
				}
				else {
					setcookie("cook_city", $cityID, time()+3600,"/",$_COOKIE["cook_city"]);
					setcookie("cook_cityname", $cityname, time()+3600,"/",$_COOKIE["cook_city"]);
						
				}
				
				/*if (isset($_SERVER['HTTP_COOKIE'])) {
					$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
					foreach($cookies as $cookie) {
						$parts = explode('=', $cookie);
						$name = trim($parts[0]);
						setcookie($name, 'cook_city', time()-1000);
						setcookie($name, 'cook_cityname', time()-1000, '/');
					}
				}*/
				
				$this->template->set_breadcrumb('Home', '/')
				//->set_breadcrumb('Category','')
				->set_breadcrumb('Region','location/region/all')
				->set_breadcrumb($cityname,'');
				
				$this->template->title($city->meta_title,$data['site_title']);
				$this->template->set_metadata('keywords', $city->meta_keyword);
				$this->template->set_metadata('description', $city->meta_description);
				$this->template->build('index',$data);
				break;

			case 'category_search':

				$prod_id = $_REQUEST['prod_id'];
				if($prod_id){
					$prod = $this->commoncall->url($_REQUEST['autocomplete']);
					$city = $_REQUEST['loca_id'];
					$data['para'] = 'SEARCH';
					
					//print $city;
					//die();
					if($city){

						$cityname = $this->location_model->get_city_data($city)->cityname;
						$this->input->set_cookie("cook_city", $city, time()+3600*30);
						$this->input->set_cookie("cook_cityname", $cityname, time()+3600*30);
					}

					$kID=0;
					
					$sql = "select * from category_tree where id = $prod_id limit 1";
					
					
					$query = $this->db->query($sql);
					if($query->result()){
						$row = $query->row();
						if($row->deep == 2){
							$kID = $row->parent_id;
						}
						else{
							$kID = $row->id;
						}
					}
					
					

					redirect('browse/practice/'.$prod.'-'.$kID);
				}
				else{
					redirect(base_url());
				}
				break;

			default:
				die('<h2>Sorry! No direct access allowed ...</h2>');
				break;
		}


	}






}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
